package com.xxx.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.modules.entity.Comment;
import com.xxx.modules.entity.Notice;
import com.xxx.modules.mapper.CommentMapper;

import com.xxx.modules.mapper.NoticeMapper;
import com.xxx.modules.service.CommentService;
import com.xxx.modules.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private NoticeMapper noticeMapper;


    @Override
    public PageInfo<Comment> selectCommentInfo(Comment comment, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();

        List<Comment> data = commentMapper.selectListInfo(comment);
        return new PageInfo<>(data);

    }

    @Override
    public List<Comment> getListByPage(Integer id) {
        List<Comment> listByPage = this.commentMapper.getListByPage(id);
        return listByPage;
    }



    @Override
    public void saveCommentInfo(Comment comment) {
        comment.setCreateTime(TimeUtil.getCurrentTime());
        commentMapper.insert(comment);
    }

    @Override
    public void updateCommentInfo(Comment comment) {
        commentMapper.updateById(comment);
    }

    @Override
    public void delCommentInfo(Integer id) {
        commentMapper.deleteById(id);
    }

    @Override
    public void delBatchCommentInfo(String idList) {
        List<String> list = Arrays.asList(idList.split(","));
        commentMapper.deleteBatchIds(list);
    }

//    @Override
//    public int addComment(Comment comment) {
//        int result = commentMapper.insert(comment);
//        if (result > 0) {
//            // 更新评论数量
//            UpdateWrapper<Notice> wrapper = new UpdateWrapper<>();
//            wrapper.setSql("comment_num = comment_num + 1");
//            wrapper.eq("id", comment.getNoticeId());
//            noticeMapper.update(null, wrapper);
//        }
//        return result;
//    }
//
//    @Override
//    public int getCommentNum(Integer noticeId) {
//        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
//        wrapper.eq("notice_id", noticeId);
//        return commentMapper.selectCount(wrapper);
//    }

}
